package cn.com.callback;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.callback.ServiceBackInvoke;
import cn.com.elite.EliteBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.XmlToJson;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class apiXMLToJson implements ServiceBackInvoke {
	private static final Log log = LogFactory.getLog(apiXMLToJson.class);

	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

		log.debug("apiXMLToJson Start:" + request);
		response.setCharacterEncoding("UTF-8");

		String callJson = "";
		String reJson = "";

		try {

			// 获取调用参数信息
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			BufferedReader iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				callJson += line;
			}

			log.debug("apiXMLToJson callXML:" + callJson);

			// 初始化Json
			reJson = XmlToJson.doTo(callJson);

			reJson = "{\"code\":\"1\",\"message\":\"成功\",\"value\":" + reJson + "}";

			log.debug("apiXMLToJson 接收返回Json:" + reJson);
			return reJson;

		} catch (Exception e) {

			log.error("apiXMLToJson Exception", e);
			reJson = "{\"code\":\"0\",\"message\":\"错误:" + e.getMessage() + "\"}";
			return reJson;
		}
	}

}